<?php
/**
 * Customer Controller
 *
 * @author Steve
 * @version
 */
require_once   BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once   BaseUtil::getModulePath("common")  .  '/services/CommonService.php';
require_once   BaseUtil::getModulePath("customer")  .  '/forms/CampusForm.php';

class Customer_CampusController extends BaseController {
    
	public function init(){
        parent::init();
	}
	
	public function indexAction(){
		$commonService = new CommonService();
		$campuses = $commonService->getCampuses($this->view,$_POST);
		$this->view->campuses = $campuses;
	}
	
	public function addAction(){
		$commonService = new CommonService();
		$provinces = $commonService->getAllProvince();
		$form = new CampusForm($provinces);
		
		$form->setAction(HttpUtil::getUrl("campus","subadd"));
		$this->view->form = $form;
	}
	
	public function subaddAction(){
		$commonService = new CommonService();
		$provinces = $commonService->getAllProvince();
		$form = new CampusForm($provinces);
		if(! $form->isValid($_POST)){
			$form->setAction(HttpUtil::getUrl("campus","subadd"));
			$this->view->form = $form;
			return $this->render("add");
		}
		$campusName = HttpUtil::getPostString("campus_name");
		$comment = HttpUtil::getPostString("comment");
		$area = HttpUtil::getPostString("area_dict");
		$visible = HttpUtil::getPostString("visible");
		$showInOrder = HttpUtil::getPostString("show_in_order");
		
		$campusArr = array(
		    "area_name"    => $campusName,
		    "comment"      => $comment,
		    "area_dict_id" => $area,
		    "visible"      => $visible,
		    "show_in_order"=> $showInOrder            
		);
		$campusId = $commonService->addCampus($campusArr);
		$this->_redirect('/customer/campus');
		exit();
	}
	
	public function editAction(){
		$campusId = $this->_getParam('id');
		$commonService = new CommonService();
		$provinces = $commonService->getAllProvince();
		$campus = $commonService->getCampusById($campusId,0);
		$form = new CampusForm($provinces,$campus);
		
		$form->setAction(HttpUtil::getUrl('campus' ,'subedit'));
		$this->view->form = $form;
		$this->view->id = $campusId; 
	}
	
	public function subeditAction(){
		$campusId = $this->_getParam('id');
		
		$commonService = new CommonService();
		$provinces = $commonService->getAllProvince();
		$campus = $commonService->getCampusById($campusId,0);
		$form = new CampusForm($provinces,$campus);
		if(! $form->isValid($_POST)){
			$form->setAction(HttpUtil::getUrl('campus' ,'subedit'));
			$this->view->form = $form;
			return $this->render("edit");
		}
		
		$campusArr['area_name'] = HttpUtil::getPostString("campus_name");
		$campusArr['comment'] = HttpUtil::getPostString("comment");
		$campusArr['area_dict_id'] = HttpUtil::getPostString("area_dict");
		$campusArr['visible'] = HttpUtil::getPostString("visible");
		$campusArr['show_in_order'] = HttpUtil::getPostString("show_in_order");
		
		$commonService->updateCampus($campusArr,$campusId);
		
		$this->_redirect('/customer/campus');
	}
	
	public function deleteAction(){
		$id = $this->_getParam('id');
		$commonService = new CommonService();
		$commonService->deleteCampus($id);
		$this->_redirect('/customer/campus');
	}
}
?>